﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraEditors;

namespace ConfExpert.Forms.Depo
{
    public partial class AylikKumasCikislari : XForm
    {
        public AylikKumasCikislari()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void AylikKumasCikislari_Load(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("GetAylikKumasCikislari");
            DataSet d = new DataSet();
            d.Tables.Add(dt);
            bs = new BindingSource(d, dt.TableName);

            //DrillDownGrid(gridView2, bs);
            //gridView2.Columns["Yil"].Visible = gridView2.Columns["Ay"].Visible = gridView2.Columns["Hafta"].Visible = gridView2.Columns["Gun"].Visible = false;
            ////gridView2.FormatAsNumber(0, "SiparisAdedi", "KesilenAdet", "YuklenenAdet", "UretimeCikanAdet", "KalanAdet");
            ////gridView2.AddSummary("SiparisAdedi", "KesilenAdet", "YuklenenAdet", "UretimeCikanAdet", "KalanAdet");
            InitPivot(pivotGridControl1, bs);
            /*
            gridView2.SetFilterTypeToCheckedList("DosyaNo");

            splitContainerControl2.AutoSize();
            ExpandCollapse(splitContainerControl2, AcKapa.Collapse);
            btnExcel.ItemClick += BtnExcel_ItemClick;*/
        }

        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        /*
        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl2.DataSource = e.CreateDrillDownDataSource();
            //gridView2.Columns["Yil"].Visible = gridView2.Columns["Ay"].Visible = gridView2.Columns["Hafta"].Visible = gridView2.Columns["Gun"].Visible = false;
            gridView2.BestFitColumns();
            ExpandCollapse(splitContainerControl2, (gridView2.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView2.MoveFirst();
        }*/

        //int areaIndex = 0;
        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;
            if (!PivotInitialized)
            {
                pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pgc.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);
                // pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);


                pgc.BeginUpdate();

                PivotGridField[] fields2 = new PivotGridField[2];

                PivotGridField fieldYear = new PivotGridField("Tarih", PivotArea.ColumnArea);
                PivotGridField fieldMonth = new PivotGridField("Tarih", PivotArea.ColumnArea);

                pgc.Fields.Add(fieldYear);
                fieldYear.GroupInterval = PivotGroupInterval.DateYear;
                fieldYear.Caption = "Yıl";

                pgc.Fields.Add(fieldMonth);
                fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
                fieldMonth.Caption = "Ay";

                fields2[0] = fieldYear;
                fields2[1] = fieldMonth;

                fields2[0].ExpandedInFieldsGroup = fields2[1].ExpandedInFieldsGroup = false;
                pgc.Groups.Add(fields2, "Tarih");

                PivotGridField[] fields3 = new PivotGridField[2] { 
                    AddNewField(pgc, "AtelyeGrubu", PivotArea.RowArea),
                    AddNewField(pgc, "Atelye", PivotArea.RowArea)};

                //pgc.Fields["AtelyeId"].FieldEdit = RI(Rp.Atelye);
                fields3[0].ExpandedInFieldsGroup = fields3[1].ExpandedInFieldsGroup = false;

                AddNewField(pgc, "Toplam", PivotArea.DataArea);

                pgc.Groups.Add(fields3, "Atelye");

                /*
                AddNewField(pgc, "KalanAdet", PivotArea.DataArea).Caption = "KALAN";
                AddNewField(pgc, "UretimeCikanAdet", PivotArea.DataArea).Caption = "Üretimde";
                AddNewField(pgc, "SiparisAdedi", PivotArea.DataArea).Caption = "Sipariş";
*/

                pgc.Fields["Toplam"].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                pgc.Fields["Toplam"].CellFormat.FormatString = "#,###";

                //.Width = 40;  .Visible = true;
                //    fld.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;

                PivotGridFormatRule newRule = new PivotGridFormatRule();
                pgc.FormatRules.Add(newRule);

                // Sets a Measure.
                newRule.Measure = pgc.Fields["Toplam"];

                // Creates a new Settings object.
                FormatRuleFieldIntersectionSettings newClass = new FormatRuleFieldIntersectionSettings();
                newRule.Settings = newClass;

                // Sets the Settings parameters.
                newClass.Column = pgc.Fields["Tarih"];
                newClass.Row = pgc.Fields["Atelye"];

                // Creates a new Rule object and defines its type.
                FormatConditionRuleDataBar formatConditionRuleDataBar = new FormatConditionRuleDataBar();
                newRule.Rule = formatConditionRuleDataBar;

                // Sets the Rule parameters.
                formatConditionRuleDataBar.PredefinedName = "Yellow Gradient";

                foreach (PivotGridField pgf in pgc.Fields)
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }
                pgc.EndUpdate();
                pgc.BestFit();

                PivotInitialized = true;
            }
        }

        private void btnExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pivotGridControl1.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            ExportPivot(pivotGridControl1);
        }
    }
}
